<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateCartsTable extends Migration
{

    /**
     * Run the migrations.
     * 购物车表
     * 外键映射：user_id -> 用户表id    product_id -> 商品id
     *
     * @return void
     */
    public function up()
    {
        Schema::create('carts', function(Blueprint $table)
        {
            $table->increments('id');
            $table->unsignedInteger('count');
            $table->unsignedInteger('user_id');
            $table->unsignedInteger('product_id');
            $table->timestamps();
        });

        Schema::table('carts', function($table) {
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('carts');
    }

}
